import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const upms = CommonConfig.upmsUrl + '/v1'

/**
 * 列举角色
 * @param [keyword] {string} 关键字
 * @returns {Promise<unknown>} promise
 */
function listRoles (keyword) {
  const url = upms + '/role_list'

  let config = {
    params: {
      name: keyword
    }
  }

  return axiosInstance.get(url, config).then(resp => {
    return Promise.resolve(resp.data)
  })
}

function listRolesPage ({ keyword, pageNum, pageSize }) {
  const url = upms + '/role_page'

  let config = {
    params: {
      name: keyword,
      pageNum,
      pageSize
    }
  }

  return axiosInstance.get(url, config).then(resp => {
    return Promise.resolve(resp.data)
  })
}
/**
 * 创建角色
 * @param name {string} 名称
 * @param remark {string} 备注
 * @return {Promise<unknown>} promise
 */
function createRole (name, remark) {
  const url = upms + '/role'

  let reqData = {
    name: name,
    remark: remark
  }

  return axiosInstance.post(url, reqData).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 编辑角色
 * @param id {number} 角色 ID
 * @param name {string} 名称
 * @param remark {string} 备注
 * @return {Promise<unknown>} promise
 */
function editRole (id, { name, remark }) {
  const url = upms + '/role'

  let reqData = {
    id,
    name,
    remark
  }

  return axiosInstance.put(url, reqData).then(resp => {
    return Promise.resolve(resp.data)
  })
}

// /v1/role_check/{id}
// 检查角色删除占用
const roleCheck = (id) => {
  const url = upms + `/role_check/${id}`
  return axiosInstance.get(url).then(resp => {
    return Promise.resolve(resp.data)
  })
}
/**
 * 删除角色
 * @param id {number} 角色 ID
 * @return {Promise<unknown>} promise
 */
function deleteRole (id) {
  const url = upms + '/role'

  let config = {
    params: {
      id
    }
  }

  return axiosInstance.delete(url, config).then(resp => {
    return Promise.resolve(resp.data)
  })
}

export default {
  listRoles,
  createRole,
  editRole,
  deleteRole,
  listRolesPage,
  roleCheck
}
